package com.googlecode.jkorpos.lingpipe;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.xml.sax.InputSource;

import com.aliasi.corpus.StringParser;
import com.aliasi.corpus.TagHandler;
import com.aliasi.io.FileExtensionFilter;
import com.aliasi.util.Strings;

public class SejongPosParser extends StringParser {
	public SejongPosParser() {
		super();
	}
	
	public SejongPosParser(TagHandler handler){
		super(handler);
	}
	
	@Override
	public void parseString(char[] cs, int start, int end) throws IOException {
		for (int i = start; i < end; i++){
			if (cs[i] == ';'){
				processSentence(cs, start, i);
				start = i;
			}
		}
	}
	
	private static Pattern p = Pattern.compile("\\S+/[A-Z]+");
	private static String[] emptyArray = new String[0];
	private void processSentence(char[] cs, int start, int end){
		CharSequence seq = new CharArray(cs, start, end);
		Matcher m = p.matcher(seq);
		List<String> tokenList = new ArrayList<String>();
		List<String> tagList = new ArrayList<String>();
		
		while (m.find()){
			String match = m.group();
			String[] tokenAndPos = match.split("/");
			tokenList.add(tokenAndPos[0]);
			tagList.add(tokenAndPos[1]);
		}
		
		((TagHandler) getHandler()).handle(
				tokenList.toArray(emptyArray), null, tagList.toArray(emptyArray));
	}
	
	@Override
	public void parse(InputSource in) throws IOException {
		in.setEncoding("UTF-16");
		super.parse(in);
	}
	
	public static void main(String[] args) {
		try {
			SejongPosParser p = new SejongPosParser(new PosPrinter());
			File[] inputs = new File("data/sejong/").listFiles(new FileExtensionFilter("txt", false));
			for (File input : inputs){
				BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(input), "utf-16"));
				StringBuilder b = new StringBuilder();
				String l;
				while (r.ready()){
					l = r.readLine();
					if (l.contains(";")){
						p.parseString(b.toString());
						b.delete(0, b.length());
					}
					b.append(l);
				}
				p.parseString(b.toString());
			}
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	private static class PosPrinter implements TagHandler{
		public void handle(String[] toks, String[] whitespaces, String[] tags) {
			for (int i = 0; i < toks.length; i++){
				System.out.println(String.format("%s/%s", toks[i], tags[i]));
			}
		}
	}

}
